use tauri::{AppHandle, Emitter, Runtime, WebviewWindowBuilder, WebviewUrl};

#[tauri::command]
pub async fn open_in_new_window<R: Runtime>(
    app: AppHandle<R>,
    path: String,
) -> Result<(), String> {
    let label = format!("window-{}", rand::random::<u32>());
    let url = WebviewUrl::App("index.html".into());
    let window = WebviewWindowBuilder::new(&app, &label, url)
        .title(&path)
        .decorations(false)
        .inner_size(1200.0, 800.0)
        .min_inner_size(800.0, 600.0)
        .build()
        .map_err(|e| e.to_string())?;

    window.show().map_err(|e| e.to_string())?;
    window.emit("set_workspace", &path).map_err(|e| e.to_string())?;

    Ok(())
}